package com.qingpeng.pay.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qingpeng.pay.R;
import com.qingpeng.pay.activity.KefuActivity;
import com.qingpeng.pay.activity.MessageActivity;
import com.qingpeng.pay.activity.MoneyActivity;
import com.qingpeng.pay.activity.SettingsActivity;
import com.qingpeng.pay.activity.TeamActivity;
import com.qingpeng.pay.activity.UpQRCodeActivity;
import com.qingpeng.pay.activity.UserInfoActivity;
import com.qingpeng.pay.api.Host;
import com.qingpeng.pay.api.RedBag;
import com.qingpeng.pay.base.BaseFragment;
import com.qingpeng.pay.beans.MessageBean;
import com.qingpeng.pay.okhttp.RetrofitCreateHelper;
import com.qingpeng.pay.utils.APP;
import com.qingpeng.pay.utils.ToastUtils;
import com.qingpeng.pay.views.MineItemView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MineFragment extends BaseFragment {
    @BindView(R.id.miv_team)
    MineItemView mivTeam;
    @BindView(R.id.text_id)
    TextView textId;
    @BindView(R.id.miv_tongzhi)
    MineItemView mivTongzhi;
    @BindView(R.id.ic_title)
    ImageView icTitle;
    private MessageBean.DataProductBean mMessageinfo;
    private boolean isCreated;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    public void onResume() {
        showData();
        if (APP.userInfo.getShenfen().equals("1")){
            icTitle.setImageResource(R.mipmap.ic_title1);
        }else if (APP.userInfo.getShenfen().equals("2")){
            icTitle.setImageResource(R.mipmap.ic_title2);
        }else{
            icTitle.setImageResource(R.mipmap.ic_title3);
        }
        super.onResume();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isCreated = true;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (!isCreated) {
            return;
        } else {
            //进行刷新操作
            showData();
        }
    }

    private void showData() {
        textId.setText("ID:  " + APP.userInfo.getUser_id());
        if (APP.userInfo.getShenfen().equals("3")) {
            mivTeam.setVisibility(View.GONE);
        } else {
            mivTeam.setVisibility(View.VISIBLE);
        }
        Call<MessageBean> zfnotion = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).Zfnotion();
        zfnotion.enqueue(new Callback<MessageBean>() {
            @Override
            public void onResponse(Call<MessageBean> call, Response<MessageBean> response) {
                MessageBean body = response.body();
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(mContext, body.getInfo());
                    return;
                }
                mMessageinfo = body.getData();
                mivTongzhi.setRightText(mMessageinfo.getMessagenum());
                initAdapter();
            }

            @Override
            public void onFailure(Call<MessageBean> call, Throwable t) {
                ToastUtils.showToast(mContext, "网络请求错误,请稍后重试");
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.miv_money, R.id.miv_team, R.id.miv_message, R.id.miv_shangma, R.id.miv_settings, R.id.miv_kefu, R.id.miv_tongzhi, R.id.miv_pingtai})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.miv_money:
                //我的账户
                startActivity(new Intent(getActivity(), MoneyActivity.class));
                break;
            case R.id.miv_team:
                //我的团队
                startActivity(new Intent(getActivity(), TeamActivity.class));
                break;
            case R.id.miv_message:
                //个人信息
                startActivity(new Intent(getActivity(), UserInfoActivity.class));
                break;
            case R.id.miv_shangma:
                //二维码管理
                startActivity(new Intent(getActivity(), UpQRCodeActivity.class));
                break;
            case R.id.miv_settings:
                //设置
                startActivity(new Intent(getActivity(), SettingsActivity.class));
                break;
            case R.id.miv_kefu:
                //客服
                startActivity(new Intent(getActivity(), KefuActivity.class));
                break;
            case R.id.miv_tongzhi:
                //个人消息和公告
                startActivity(new Intent(getActivity(), MessageActivity.class));
                break;
            case R.id.miv_pingtai:
//                startActivity(new Intent(getActivity(), MessageActivity.class));
                break;
        }
    }
}
